//index.js
//获取应用实例
const app = getApp()



Page({
  data: {
    showBottomWordText: false,
    motto: 'Hello World',
    userInfo: {},

    startCity:'',
    endCity:'',
    hasUserInfo: false,
    navActive:true,
    firstResult: 1,
    isDown:true,


    RequestParameters: {
      startingPlace: '',
      destination: '',
      firstResult: 1 ,
      shareType: '',
    },

    articleList: {
      list:[]
    },
    tate:{
      text:'123114'
    },
    navList:[
    '北京1',
    '北京2',
    '北京3',
    '北京4',
    '北京5',
    '北京6',
    '北京7',
    '北京8',
    '北京9',
    '北京0'
    ]
  },

  getArticleList(){
    let self = this;

    wx.showLoading({
      title: '加载中',
    })

    wx.request({
      url: app.globalData.serverUrl+'CarShareMessage/queryCarShareMessageByPlace.do', 
      data: self.data.RequestParameters,
      success: function(res) {

        if (res.data.state == 1) {
          console.log(res.data.data)
          let yuanlist = self.data.articleList.list;
          for (var i = 0; i < res.data.data.CarShareMessageList.length; i++) {
            yuanlist.push(res.data.data.CarShareMessageList[i])   
          }
          
          self.setData({
            'articleList.list': yuanlist,
            'RequestParameters.firstResult': self.data.RequestParameters.firstResult +1,
            isDown: true
          })
        }else{
          self.setData({
            showBottomWordText: true
          })
        }
        wx.hideLoading()
        // self.globalData.serverOpenInfo = res.data
      }
    })

  },
  setCityval(e){
    if (e.currentTarget.dataset.city) {
      this.setData({
        startCity : e.detail.value
      })
    }else{
      this.setData({
        endCity : e.detail.value
      })
    }
  },
  getGpsFun(){
    let self = this;
    app.getAddress(function(data) {
      self.setData({
        startCity: data.result.address_component.city,
      })
    })
  },
  toggleNav(event){
    this.setData({
      'navActive': event.currentTarget.dataset.status,
      'RequestParameters.firstResult': 1,
      'RequestParameters.shareType': event.currentTarget.dataset.status ? '0' : '1'

    })
    this.getArticleList()
  },
  toggleRess(){
    let startCity = this.data.startCity;
    this.setData({
      startCity: this.data.endCity,
      endCity: startCity
    })

  },
  searchFun(){
    //搜索接口 列表
    // console.log(this.data.startCity);
    // console.log(this.data.endCity);

    if (this.data.startCity.length >= 2) {
      if (this.data.endCity.length  >= 2) {
        this.setData({
          'RequestParameters.firstResult': 1,
          'RequestParameters.startingPlace': self.data.startCity,
          'RequestParameters.destination': self.data.endCity,
        })
        this.getArticleList()
      }else{
        wx.showToast({title: '填写目的地',icon: 'none'})
      }
    }else{
      wx.showToast({title: '填写出发地',icon: 'none'})
    }
  },
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    console.log('监听页面显示')
    var array = new Array()
    this.setData({
      'articleList.list': [],
      'RequestParameters.firstResult' : 1
    })

    this.getArticleList();

  },

  onLoad: function () {
    let self = this;
    this.getGpsFun();
    // this.getArticleList();

    

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (wx.canIUse('button.open-type.getUserInfo')){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onReachBottom: function () {
    console.log('上拉')
    if (this.data.isDown) {
      this.setData({
        isDown: false
      })
      this.getArticleList();
    }



  },
  onShareAppMessage: function () {
    return {
      title: '城际顺风车拼车',
      path: '/pages/index/index?openid='+ app.globalData.serverOpenInfo.openid
    }
  }
})
